#include "core_commands.h"
#include "../mem/mem_manager.h"

#include <windows.h>

typedef int (*TCmdPtr)(const char **, int);

typedef struct
{
	char *cmdName;
	TCmdPtr cmdPtr;
}TCommand;

TCommand *commands;

int cmd_moveForward(const char **, int)
{
	MessageBox(NULL, "Forward", "action", MB_OK);
	return 0;
}

int cmd_moveBackward(const char **, int)
{
	MessageBox(NULL, "Backward", "action", MB_OK);
	return 0;
}

void setCommand(int cmdID, const char *name, TCmdPtr cmdPtr)
{
	g_strdup(commands[cmdID].cmdName, name);
	commands[cmdID].cmdPtr = cmdPtr;
}

int initCommands()
{
	g_allocv(commands, TCommand, CMD_MAXCOMMANDS);
	for (int i=0;i<CMD_MAXCOMMANDS;i++)
	{
		commands[i].cmdName = NULL;
		commands[i].cmdPtr = NULL;
	}

	setCommand(CMD_MOVEFORWARD, "MOVE_FORWARD", cmd_moveForward);
	setCommand(CMD_MOVEBACKWARD, "MOVE_BACKWARD", cmd_moveBackward);

	return 0;
}

int shutdownCommands()
{
	for (int i=0;i<CMD_MAXCOMMANDS;i++)
		g_free(commands[i].cmdName);

	g_free(commands);
	return 0;
}

int getCommandID(const char *name)
{
	for (int i=0;i<CMD_MAXCOMMANDS;i++)
		if (!stricmp(commands[i].cmdName, name))
			return i;

	return -1;
}

int execCommand(int cmdID, const char **cmdParams, int cmdCount)
{
	switch (cmdID)
	{
	case CMD_MOVEFORWARD:
		return cmd_moveForward(cmdParams, cmdCount);
	case CMD_MOVEBACKWARD:
		return cmd_moveForward(cmdParams, cmdCount);

	default:
		return 0;
	}
}

int execCommand(char* cmd)
{
	return 0;
}